package com.m20studio.mop.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;

import com.m20studio.mop.R;

/**
 * @Description:
 * @作者:Clark.Zhu
 * @时间:2012 2012-4-11 上午10:33:48
 * @项目名:M20_MOP
 */

public class Help extends Activity implements android.view.GestureDetector.OnGestureListener {
	private int[] imgs = { R.drawable.help_img1, R.drawable.help_img2,
			  R.drawable.help_img3, R.drawable.help_img4, R.drawable.help_img5 };

	private GestureDetector gestureDetector = null;
	private ViewFlipper viewFlipper = null;
	private Activity mActivity = null;
	private LinearLayout goToBtn;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.help);

		mActivity = this;

		goToBtn = (LinearLayout) findViewById(R.id.goto_btn);
		goToBtn.setOnClickListener(new View.OnClickListener() { 
        	public void onClick(View v){
        		Intent intent = new Intent(Help.this, ClassTZ.class); 
          		intent.putExtra("lmId", 0);
          		intent.putExtra("classId", 0);
          		Help.this.startActivity(intent);
          		Help.this.finish();
	        } 
		});
		viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);
		gestureDetector = new GestureDetector(this);
		for (int i = 0; i < imgs.length; i++) {			
			ImageView iv = new ImageView(this);
			iv.setImageResource(imgs[i]);
			iv.setScaleType(ImageView.ScaleType.FIT_XY);
			viewFlipper.addView(iv, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));		
		}

		viewFlipper.setAutoStart(true);	
		viewFlipper.setFlipInterval(3000);
		if(viewFlipper.isAutoStart() && !viewFlipper.isFlipping()){
			viewFlipper.startFlipping();
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		viewFlipper.stopFlipping();
		viewFlipper.setAutoStart(false);
		return gestureDetector.onTouchEvent(event);
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		if (e2.getX() - e1.getX() > 120) {
			Animation rInAnim = AnimationUtils.loadAnimation(mActivity, R.anim.push_right_in);
			Animation rOutAnim = AnimationUtils.loadAnimation(mActivity, R.anim.push_right_out);
			viewFlipper.setInAnimation(rInAnim);
			viewFlipper.setOutAnimation(rOutAnim);
			viewFlipper.showPrevious();
			return true;
		} else if (e2.getX() - e1.getX() < -120) {
			Animation lInAnim = AnimationUtils.loadAnimation(mActivity, R.anim.push_left_in);
			Animation lOutAnim = AnimationUtils.loadAnimation(mActivity, R.anim.push_left_out);
			viewFlipper.setInAnimation(lInAnim);
			viewFlipper.setOutAnimation(lOutAnim);
			viewFlipper.showNext();
			return true;
		}
		return true;
	}
	
	//退出提示框
  	private void showExitAlert() {
  		final AlertDialog dlg = new AlertDialog.Builder(this).create();
  		dlg.show();
  		Window window = dlg.getWindow();
  	    // *** 主要就是在这里实现这种效果的.
  	    // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
  		window.setContentView(R.layout.exit);
  	    // 为确认按钮添加事件,执行退出应用操作
  		ImageButton ok = (ImageButton) window.findViewById(R.id.btn_ok);
  		ok.setOnClickListener(new View.OnClickListener() {
  			public void onClick(View v) {
  				Intent startMain = new Intent(Intent.ACTION_MAIN);
  		        startMain.addCategory(Intent.CATEGORY_HOME);
  		        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  		        startActivity(startMain);
  		        System.exit(0);
  			}
  		});
  		// 关闭alert对话框架
        ImageButton cancel = (ImageButton) window.findViewById(R.id.btn_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		dlg.cancel();
        	}
	  });
  	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		if(keyCode == KeyEvent.KEYCODE_BACK){				
  			showExitAlert();				  			
  		}
		return false;
	}

	public boolean onDown(MotionEvent e) {
		return false;
	}

	public void onLongPress(MotionEvent e) {
	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		return false;
	}

	public void onShowPress(MotionEvent e) {
	}

	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}
}

